<template>
  <div class="app-container">
    <log-content-component
      ref="serverLogComponent"
      :log-content="logContent"
    />
  </div>
</template>

<script>
import * as APIS from '@/api/server/index'
import { Notification } from 'element-ui'
import { mapGetters } from 'vuex'
import LogContentComponent from '@/views/deploy/proccess/components/LogContentComponent'

export default {
  name: 'K8sPodLog',
  components: {
    LogContentComponent
  },
  data() {
    return {
      projectId: -1,
      logContent: '',
      podName: null,
      setIntRequest: null
    }
  },
  computed: {
    ...mapGetters([
      'env'
    ])
  },
  watch: {
  },
  created() {
    const query = this.$route.query
    if (query === undefined || query.projectId === undefined) {
      this.$notify({
        title: 'url中必须带上projectId参数',
        type: 'error',
        duration: 2500
      })
      return
    }

    if (query.podName === undefined) {
      this.$notify({
        title: 'url中必须带上podName参数',
        type: 'error',
        duration: 2500
      })
      return
    }
    this.projectId = parseInt(query.projectId)
    this.podName = query.podName
  },
  mounted() {
    this.$bus.$on('global-env-change', data => {})
    this.queryLog()
    this.$nextTick(() => {
      this.startTimer()
    })
  },
  beforeDestroy() {
    this.$bus.$off('global-env-change')
    this.stopIntRequest()
  },
  methods: {
    queryLog() {
      APIS.getPodLog({
        projectId: this.projectId,
        env: this.env,
        podName: this.podName
      }).then(res => {
        this.logContent = res
      }).finally(() => {
      }).catch(err => {
        Notification.error({
          title: err,
          duration: 4000
        })
      })
    },
    startTimer() {
      // 时间事件，
      const _this = this
      this.setIntRequest = setInterval(() => {
        if (_this.projectId) {
          _this.queryLog()
        }
      }, 2000) // 每隔10秒 10000ms
    },
    stopIntRequest() {
      this.setIntRequest && clearInterval(this.setIntRequest)
      this.setIntRequest = null // 内存回收
    }
  }
}
</script>

<style scoped>
</style>
